/*
 * Copyright 2014 Toxic Bakery
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ToxicBakery.pagetransformer.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.CommonDialog;

import com.ToxicBakery.pagetransformer.ResourceTable;
import com.ToxicBakery.pagetransformer.bean.TransformerItem;
import com.ToxicBakery.pagetransformer.page.ListProvider;
import com.ToxicBakery.pagetransformer.page.PageProvider;
import com.ToxicBakery.viewpager.transformer.AccordionTransformer;
import com.ToxicBakery.viewpager.transformer.BackgroundToForegroundTransformer;
import com.ToxicBakery.viewpager.transformer.CubeInTransformer;
import com.ToxicBakery.viewpager.transformer.CubeOutTransformer;
import com.ToxicBakery.viewpager.transformer.DefaultTransformer;
import com.ToxicBakery.viewpager.transformer.DepthPageTransformer;
import com.ToxicBakery.viewpager.transformer.DrawerTransformer;
import com.ToxicBakery.viewpager.transformer.FlipHorizontalTransformer;
import com.ToxicBakery.viewpager.transformer.FlipVerticalTransformer;
import com.ToxicBakery.viewpager.transformer.ForegroundToBackgroundTransformer;
import com.ToxicBakery.viewpager.transformer.RotateDownTransformer;
import com.ToxicBakery.viewpager.transformer.RotateUpTransformer;
import com.ToxicBakery.viewpager.transformer.ScaleInOutTransformer;
import com.ToxicBakery.viewpager.transformer.StackTransformer;
import com.ToxicBakery.viewpager.transformer.TabletTransformer;
import com.ToxicBakery.viewpager.transformer.ZoomInTransformer;
import com.ToxicBakery.viewpager.transformer.ZoomOutSlideTransformer;
import com.ToxicBakery.viewpager.transformer.ZoomOutTransformer;
import com.ToxicBakery.viewpager.view.CollPageSlide;

import java.util.ArrayList;

/**
 * 入口类
 *
 * @since 2021-04-12
 */
public class MainAbilitySlice extends AbilitySlice {
    private CollPageSlide mPageSlider;
    private CommonDialog dialog;
    private Button selectBtn;
    private Image image;
    private ArrayList<TransformerItem> transformers = new ArrayList<>();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initData();
        initPages();
        selectBtn = (Button) findComponentById(ResourceTable.Id_btn_select);
        selectBtn.setText("DefaultTransformer");
        image = (Image) findComponentById(ResourceTable.Id_icon);
        selectBtn.setClickedListener(component -> showSelectDialog());
    }

    private void initData() {
        // 初始化Item数据 动画数据
        transformers.add(new TransformerItem(DefaultTransformer.class));
        transformers.add(new TransformerItem(AccordionTransformer.class));
        transformers.add(new TransformerItem(BackgroundToForegroundTransformer.class));
        transformers.add(new TransformerItem(CubeInTransformer.class));
        transformers.add(new TransformerItem(CubeOutTransformer.class));
        transformers.add(new TransformerItem(DepthPageTransformer.class));
        transformers.add(new TransformerItem(FlipHorizontalTransformer.class));
        transformers.add(new TransformerItem(FlipVerticalTransformer.class));
        transformers.add(new TransformerItem(ForegroundToBackgroundTransformer.class));
        transformers.add(new TransformerItem(RotateDownTransformer.class));
        transformers.add(new TransformerItem(RotateUpTransformer.class));
        transformers.add(new TransformerItem(ScaleInOutTransformer.class));
        transformers.add(new TransformerItem(StackTransformer.class));
        transformers.add(new TransformerItem(TabletTransformer.class));
        transformers.add(new TransformerItem(ZoomInTransformer.class));
        transformers.add(new TransformerItem(ZoomOutSlideTransformer.class));
        transformers.add(new TransformerItem(ZoomOutTransformer.class));
        transformers.add(new TransformerItem(DrawerTransformer.class));
    }


    private void initPages() {
        mPageSlider = (CollPageSlide) findComponentById(ResourceTable.Id_page_slide);
        PageProvider pageProvider = new PageProvider(getContext());
        mPageSlider.setProvider(pageProvider);
        mPageSlider.setPages(pageProvider.getComponents());
    }

    private void showSelectDialog() {
        dialog = new CommonDialog(getContext());
        ComponentContainer rootView = (ComponentContainer) LayoutScatter.getInstance(this)
            .parse(ResourceTable.Layout_diallog_layout, null, false);
        applyList(rootView);
        dialog.setContentCustomComponent(rootView);
        dialog.setAutoClosable(true);
        if (!dialog.isShowing()) {
            dialog.show();
        }
        image.setPixelMap(ResourceTable.Media_up);
        dialog.setDialogListener(() -> {
            image.setPixelMap(ResourceTable.Media_down);
            return false;
        });
    }

    private void applyList(ComponentContainer rootView) {
        ListContainer listContainer = (ListContainer) rootView.findComponentById(
            ResourceTable.Id_listContainer);
        listContainer.setItemProvider(new ListProvider(transformers));
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int index, long l) {
                TransformerItem transformerItem = transformers.get(index);
                mPageSlider.setPageTransformer(transformerItem.getPageTransformer());
                selectBtn.setText(transformerItem.getName());
                image.setPixelMap(ResourceTable.Media_down);
                dialog.destroy();
            }
        });
    }

}
